using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace Gadgets.Trading
{
    public partial class eTradingBook : WeifenLuo.WinFormsUI.Docking.DockContent 
    {
        public eTradingBook()
        {
            InitializeComponent();
        }

        private void eTradingBook_Load(object sender, EventArgs e)
        {
            DataSet data = new DataSet();
            DataTable table = new DataTable("GridView");
            foreach (Xceed.Grid.Column col in gridControl1.Columns)
            {
                table.Columns.Add(new DataColumn(col.FieldName, col.DataType));
            }
            data.Tables.Add(table);
            gridControl1.SetDataBinding(data, "GridView");
            colORDER_DATE.FormatSpecifier = DeclareLibrary.DATE_DISPLAY_FORMAT;
            colORDER_QTTY.FormatSpecifier = DeclareLibrary.NUMBER_FORMAT;
            colCANCEL_QTTY.FormatSpecifier = DeclareLibrary.NUMBER_FORMAT;
            colTRANS_QTTY.FormatSpecifier = DeclareLibrary.NUMBER_FORMAT;
            colTRANS_VALUE.FormatSpecifier = DeclareLibrary.CURRENCY_FORMAT;
        }

        private void eTradingBook_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.F9)
            {
                
                string client_id = string.Empty;
                string symbol = string.Empty;
                DateTime order_date = DeclareLibrary.MARKET_DAY;
                string board_id = string.Empty;
                string side = string.Empty;
                if (valueRow1.Cells["ACCOUNT_NO"].Value != null)
                    client_id = valueRow1.Cells["ACCOUNT_NO"].Value.ToString();
                if (valueRow1.Cells["SYMBOL"].Value != null)
                    symbol = valueRow1.Cells["SYMBOL"].Value.ToString();                
                if (valueRow1.Cells["BOARD_ID"].Value != null)
                    board_id = valueRow1.Cells["BOARD_ID"].Value.ToString();
                if (valueRow1.Cells["SIDE"].Value != null)
                    side = valueRow1.Cells["SIDE"].Value.ToString();

                DataSet normal_order = ABSCommonLibrary.DoInquiry(
                    CommonLibrary.CommonLibrary.BOSC002002,
                    new object[4] {board_id, client_id, 
                        symbol, DeclareLibrary.MARKET_DAY});
                gridControl1.SetDataBinding(normal_order, normal_order.Tables[0].TableName);
            }
            else if (e.KeyCode == Keys.F10)
            {
                SaveFileDialog saveFileDialog = new SaveFileDialog();
                saveFileDialog.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.Personal);
                saveFileDialog.Filter = "Excel Files (*.xls)|*.xls";
                saveFileDialog.AddExtension = true;
                if (saveFileDialog.ShowDialog(this) == DialogResult.OK)
                {
                    string FileName = saveFileDialog.FileName;
                    // TODO: Add code here to save the current contents of the form to a file.
                    Xceed.Grid.Exporting.ExcelExporter export = new Xceed.Grid.Exporting.ExcelExporter();
                    export.Export(gridControl1, FileName);
                }
            }
        }

        private void toolStripMenuCancel_Click(object sender, EventArgs e)
        {
            try
            {
                string sQuestion = string.Empty;
                if (gridControl1.DataRows.Count == 0)
                    return;
                Xceed.Grid.CellRow row = gridControl1.CurrentCell.ParentRow;
                switch (row.Cells["SIDE"].Value.ToString())
                {
                    case "S":
                        sQuestion = "ban ";
                        break;
                    case "B":
                        sQuestion = "mua ";
                        break;
                    default:
                        break;
                }
                sQuestion += row.Cells["ORDER_QTTY"].Value.ToString() +
                    " " + row.Cells["SYMBOL"].Value.ToString() +
                    " gia " + row.Cells["ORDER_PRICE"].Value.ToString() +
                    " tai khoan " + row.Cells["ACCOUNT_NO"].Value.ToString();

                if (ABSCommonLibrary.ShowQuestion(
                    "Ban muon huy lenh " + sQuestion + " ?") == DialogResult.No)
                    return;
                object[] p_param = new object[4];

                if (row.Cells["ORDER_ID"].Value.ToString() != string.Empty)
                    p_param[0] = row.Cells["ORDER_ID"].Value;
                else
                    p_param[0] = 0;

                if (row.Cells["ORDER_NUMBER"].Value.ToString() != string.Empty)
                    p_param[1] = row.Cells["ORDER_NUMBER"].Value;
                else
                    p_param[1] = string.Empty;
                p_param[2] = DeclareLibrary.USER_NAME;
                p_param[3] = DeclareLibrary.MARKET_DAY;

                int error_code = Convert.ToInt32(ABSCommonLibrary.DoExecute(
                    CommonLibrary.CommonLibrary.BOSC002013, ref p_param));
                switch (error_code)
                {
                    case -1:
                        ABSCommonLibrary.ShowMessage("Lenh chua duoc confirm boi so giao dich ck");
                        break;
                    case -2:
                        ABSCommonLibrary.ShowMessage("Lenh huy bi tu choi do da duoc khop tat ca");
                        break;
                    case -3:
                        ABSCommonLibrary.ShowMessage("Lenh huy bi tu choi do da ton tai lenh huy");
                        break;
                    case -4:
                        ABSCommonLibrary.ShowMessage("Lenh huy bi tu choi trong phien khop lenh dinh ky");
                        break;
                    default:
                        ABSCommonLibrary.ShowMessage("Lenh huy " + sQuestion + " da duoc gui lenh so giao dich ck");
                        break;
                }
            }
            catch (Exception ex)
            {
                ABSCommonLibrary.ShowErrorMessage(ex.ToString());
            }            
        }

        private void toolStripMenuChange_Click(object sender, EventArgs e)
        {
            if (gridControl1.DataRows.Count == 0)
                return;
            try
            {
                Xceed.Grid.CellRow row = gridControl1.CurrentCell.ParentRow;
                object[] p_param = new object[4];
                int error_code = 0;
                Form obj;
                if (row.Cells["BOARD_ID"].Value.ToString() == "HOSE")
                {
                    obj = new Common.ChangeAccountNumber();
                    ((Common.ChangeAccountNumber)obj).AccountNo = row.Cells["ACCOUNT_NO"].Value.ToString();
                }
                else
                {
                    obj = new Gadgets.Common.ChangeOrderPrice();
                    ((Common.ChangeOrderPrice)obj).NewPrice = Convert.ToDecimal(row.Cells["QUOTE_PRICE"].Value);
                }
                                
                if (obj.ShowDialog() != DialogResult.OK)
                    return;
                if (row.Cells["BOARD_ID"].Value.ToString() == "HOSE")
                {
                    if (row.Cells["ORDER_NUMBER"].Value.ToString() != string.Empty)
                        p_param[0] = row.Cells["ORDER_NUMBER"].Value;
                    else
                        p_param[0] = string.Empty;
                    p_param[1] = ((Common.ChangeAccountNumber)obj).AccountNo;
                    p_param[2] = DeclareLibrary.USER_NAME;
                    p_param[3] = DeclareLibrary.MARKET_DAY;
                    error_code = Convert.ToInt32(ABSCommonLibrary.DoExecute(
                        CommonLibrary.CommonLibrary.BOSC002005, ref p_param));
                }else{
                    if (row.Cells["ORDER_NUMBER"].Value.ToString() != string.Empty)
                        p_param[0] = row.Cells["ORDER_NUMBER"].Value;
                    else
                        p_param[0] = string.Empty;
                    p_param[1] = ((Common.ChangeOrderPrice)obj).NewPrice;
                    p_param[2] = DeclareLibrary.USER_NAME;
                    p_param[3] = DeclareLibrary.MARKET_DAY;
                    error_code = Convert.ToInt32(ABSCommonLibrary.DoExecute(
                        CommonLibrary.CommonLibrary.BOSC002015, ref p_param));
                }
                switch (error_code)
                {
                    case -1:
                        ABSCommonLibrary.ShowMessage("Lenh chua duoc confirm boi so giao dich ck");
                        break;
                    case -2:
                        ABSCommonLibrary.ShowMessage("Lenh sua bi tu choi do da duoc khop tat ca");
                        break;
                    case -3:
                        ABSCommonLibrary.ShowMessage("Lenh sua bi tu choi do da ton tai lenh huy");
                        break;
                    case -4:
                        ABSCommonLibrary.ShowMessage("Lenh sua bi tu choi doi voi lenh gia ATO, ATC");
                        break;
                    case -5:
                        ABSCommonLibrary.ShowMessage("lenh sua gia vuot gia tran, san");
                        break;
                    case -6:
                        ABSCommonLibrary.ShowMessage("Khong dat lenh sua gia tai HOSE");
                        break;
                    case -7:
                        ABSCommonLibrary.ShowMessage("Khong dat lenh sua so tai khoan tai HNX");
                        break;
                    case -8:
                        ABSCommonLibrary.ShowMessage("Chi duoc phep sua so tai khoan trong phien lien tuc");
                        break;
                    case -9:
                        ABSCommonLibrary.ShowMessage("Lenh da bi huy tren san giao dich");
                        break;
                    case -10:
                        ABSCommonLibrary.ShowMessage("Lenh da bi khop tren san giao dich");
                        break;
                    case -11:
                        ABSCommonLibrary.ShowMessage("So tai khoan khong ton tai tren he thong");
                        break;
                    default:
                        ABSCommonLibrary.ShowMessage("Lenh sua da duoc gui den so giao dich ck");
                        break;
                }
            }
            catch (Exception ex)
            {
                ABSCommonLibrary.ShowErrorMessage(ex.ToString());
            }
            
        }
    }
}